﻿using MxWeiXinPF.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MxWeiXinPF.Web.shop
{
    public partial class userratepoints_list : MxWeiXinPF.Web.UI.UserPage
    {
        public string carturl = "";
        public string indexurl = "";
        protected int page;
        protected int pageSize;
        protected int totalCount;
        protected int uid = 0;
        public string url = "";
        protected MxWeiXinPF.Model.users user_info;
        public string userinfourl = "";
        public string categoryurl = "";
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            int num = 0;
            int num2 = 0;
            MxWeiXinPF.BLL.user_rate _rate = new MxWeiXinPF.BLL.user_rate();
            for (int i = 0; i < this.rptList.Items.Count; i++)
            {
                int id = Convert.ToInt32(((HiddenField)this.rptList.Items[i].FindControl("hidId")).Value);
                CheckBox box = (CheckBox)this.rptList.Items[i].FindControl("chkId");
                if (box.Checked)
                {
                    if (_rate.AuditExists(id))
                    {
                        num2++;
                    }
                    else if (_rate.Delete(id))
                    {
                        num++;
                    }
                    else
                    {
                        num2++;
                    }
                }
            }
            this.RptBind("a.id>0" + this.CombSqlTxt(this.uid), "id desc");
            this.Page.ClientScript.RegisterStartupScript(this.Page.ClientScript.GetType(), "myscripts", string.Concat(new object[] { "<script>alert(\"删除成功", num, "条，失败", num2, "条\");</script>" }));
        }

        protected string CombSqlTxt(int uid)
        {
            StringBuilder builder = new StringBuilder();
            if (!string.IsNullOrEmpty(base.openid))
            {
                builder.Append(" and a.uid='" + uid + "' ");
            }
            return builder.ToString();
        }

        private int GetPageSize(int _default_size)
        {
            int num;
            if (int.TryParse(Utils.GetCookie("userratepoints_list_page_size"), out num) && (num > 0))
            {
                return num;
            }
            return _default_size;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                base.wid = MyCommFun.RequestInt("wid");
                base.openid = MyCommFun.RequestOpenid();
                this.shopid = MyCommFun.RequestInt("shopid");
                this.indexurl = string.Concat(new object[] { "index.aspx?wid=", base.wid, "&openid=", this.openid, "&shopid=", shopid });
                this.carturl = string.Concat(new object[] { "cart.aspx?wid=", base.wid, "&openid=", this.openid, "&shopid=", shopid });
                this.userinfourl = string.Concat(new object[] { "userinfo.aspx?wid=", base.wid, "&openid=", this.openid, "&shopid=", shopid });
                this.categoryurl = MyCommFun.urlAddOpenid("/shop/category.aspx?wid=" + base.wid + "&shopid=" + shopid, base.openid);
                this.uid = MyCommFun.RequestInt("uid");
                this.pageSize = this.GetPageSize(10);
                if (this.uid == 0)
                {
                    if (HttpContext.Current.Session["dt_session_user_info"] != null)
                    {
                        this.user_info = HttpContext.Current.Session["dt_session_user_info"] as MxWeiXinPF.Model.users;
                        this.uid = this.user_info.id;
                    }
                    else
                    {
                        MxWeiXinPF.Model.users modelByOpenid = new MxWeiXinPF.Model.users();
                        modelByOpenid = new MxWeiXinPF.BLL.users().GetModelByOpenid(base.openid);
                        this.uid = modelByOpenid.id;
                    }
                    if (!this.Page.IsPostBack)
                    {
                        this.RptBind("a.id>0" + this.CombSqlTxt(this.uid), "id desc");
                    }
                }
            }
            catch (Exception ex)
            {

            }

        }

        private void RptBind(string _strWhere, string _orderby)
        {
            this.page = MXRequest.GetQueryInt("page", 1);
            this.rptList.DataSource = new MxWeiXinPF.BLL.user_rate().GetListScore(base.wid, this.pageSize, this.page, _strWhere, _orderby, out this.totalCount);
            this.rptList.DataBind();
            this.txtPageNum.Text = this.pageSize.ToString();
            string linkUrl = Utils.CombUrlTxt("userratepoints_list.aspx", "uid={0}", new string[] { this.uid.ToString() });
            this.PageContent.InnerHtml = Utils.OutPageList(this.pageSize, this.page, this.totalCount, linkUrl, 8);
        }

        protected void rptList_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            int id = int.Parse(e.CommandArgument.ToString());
            MxWeiXinPF.BLL.user_rate _rate = new MxWeiXinPF.BLL.user_rate();
            if (e.CommandName == "Del")
            {
                if (_rate.AuditExists(id))
                {
                    this.Page.ClientScript.RegisterStartupScript(this.Page.ClientScript.GetType(), "myscripts", "<script>alert(\"已完成兑换的记录不能删除！\");</script>");
                }
                else if (_rate.Delete(id))
                {
                    this.RptBind("a.id>0" + this.CombSqlTxt(this.uid), "id desc");
                    this.Page.ClientScript.RegisterStartupScript(this.Page.ClientScript.GetType(), "myscripts", "<script>alert(\"删除成功！\");</script>");
                }
            }
        }

        protected void txtPageNum_TextChanged(object sender, EventArgs e)
        {
            int num;
            if (int.TryParse(this.txtPageNum.Text.Trim(), out num) && (num > 0))
            {
                Utils.WriteCookie("userratepoints_list", num.ToString(), 0x3840);
            }
            base.Response.Redirect(Utils.CombUrlTxt("userratepoints_list.aspx", "uid={0}", new string[] { this.uid.ToString() }));
        }

        public string JifenConvert(string str)
        {
            if (string.IsNullOrEmpty(str))
            {
                return "0.0";
            }
            decimal jf = Convert.ToDecimal(str);
            if (jf < 0)
            {
                return (-jf).ToString();
            }
            return str;
        }
    }
}